package com.cpi.basilos.warehouse.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

import com.cpi.basilos.warehouse.exception.WarehouseException;
import com.cpi.basilos.warehouse.form.WarehouseByLocationForm;
import com.cpi.basilos.warehouse.service.WarehouseService;
import com.cpi.basilos.warehouse.view.WarehouseByLocationView;
import com.cpi.user.view.UserView;
import com.cs.common.exception.CSException;
import com.cs.common.struts.Actionable;
/**
 * 
 * @author joao
 *
 */
public class WarehouseByLocationAction extends MappingDispatchAction implements Actionable {

	public ActionForward inventoryByUbication(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String forward = DEFAULT_FORWARD;	
		WarehouseService warehouseService = new WarehouseService();
		HttpSession httpSession = request.getSession();
		UserView userView = null;
		try{
			userView = (UserView)httpSession.getAttribute(USER_SESSION_NAME);
			int locationId = userView.getWarehouseId();
			int cornTypeId = Integer.parseInt(request.getParameter("cornTypeId"));
			
			WarehouseByLocationForm wform = (WarehouseByLocationForm) form;
			WarehouseByLocationView view = warehouseService.inventoryByUbication(locationId, cornTypeId);
			wform.setWarehouseByLocationView(view);
			
		}catch(Exception exception){
			WarehouseException warehouseException = null;
			exception.printStackTrace(System.out);
			warehouseException = new WarehouseException(exception , CSException.LAYER_ACTION,CSException.ACTION_SELECT);
			form.reset(mapping, request);
			httpSession.setAttribute(CUSTOM_EXCEPTION, warehouseException);
			throw warehouseException;
		}
		return mapping.findForward(forward);
	}
	
}
